﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace KAsFrame.HttpAPI
{
    // 本来应该放到common类库，可是不知道为啥AddDistributedMemoryCache总是找不到
    // 为什么要使用分布式缓存：基于微服务框架需求考虑，分布式缓存有利于不同服务间的串联
    public static class StackExchangeRedisExtensions
    {
        /// <summary>
        /// 基于IDistributedCache,添加Redis缓存服务
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        public static void AddStackExchangeRedis(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddDistributedMemoryCache();
            services.AddStackExchangeRedisCache(options =>
            {
                options.Configuration = configuration.GetValue<string>("Redis:ConnectionString");
                options.InstanceName = configuration.GetValue<string>("Redis:InstanceName");
            });
        }
    }
}
